package com.oa.modules.approvalsec1.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.Query;
import com.oa.modules.approvalsec1.entity.Operationlog;
import com.oa.modules.approvalsec1.dao.OperationlogDao;
import com.oa.modules.approvalsec1.service.OperationlogService;
import org.apache.shiro.SecurityUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Operationlog)表服务实现类
 *
 * @author makejava
 * @since 2020-11-20 11:23:58
 */
@Service("operationlogService")
public class OperationlogServiceImpl extends ServiceImpl<OperationlogDao,Operationlog> implements OperationlogService {
    @Resource
    private OperationlogDao operationlogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Operationlog queryById(Long id) {
        return this.operationlogDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Operationlog> queryAllByLimit(int offset, int limit) {
        return this.operationlogDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param operationlog 实例对象
     * @return 实例对象
     */
    @Override
    public Operationlog insert(Operationlog operationlog) {
        this.operationlogDao.insert(operationlog);
        return operationlog;
    }

    /**
     * 修改数据
     *
     * @param operationlog 实例对象
     * @return 实例对象
     */
    @Override
    public Operationlog update(Operationlog operationlog) {
        this.operationlogDao.update(operationlog);
        return this.queryById(operationlog.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.operationlogDao.deleteById(id) > 0;
    }

    @Override
    public boolean packOperation(String operation) {
        Operationlog operationlog = new Operationlog();
        operationlog.setOperationTime(new Date());
        operationlog.setOperation(operation);
        //用户名
        Object username = SecurityUtils.getSubject().getPrincipal();
        JSONObject jsonObject = new JSONObject(username);
        if(jsonObject.has("userid")){
            operationlog.setUserId(jsonObject.get("userid").toString());
        }else{
            operationlog.setUserId(jsonObject.get("userId").toString());
        }
        if(jsonObject.has("username")){
            operationlog.setUserName(jsonObject.get("username").toString());
        }else{
            operationlog.setUserName(jsonObject.get("userName").toString());
        }
        return this.save(operationlog);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //return new PageUtils(this.page(new Query<Operationlog>().getPage(params), AppUtils.setQueryWrapper(new QueryWrapper<Operationlog>(), AppUtils.checkMap(params), "id", false)));
        IPage<Operationlog> page = new Query<Operationlog>().getPage(params);
        page.setPages(page.getCurrent() - 1);
        params = AppUtils.checkMap(params);
        return AppUtils.setPage(page, baseMapper.findPage(params), baseMapper.findRecords(page, params));
    }
}
